//
//  DYZSiftController.m
//  findCar
//
//  Created by 毕仲阳 on 16/8/20.
//  Copyright © 2016年 mycompany. All rights reserved.
//

#import "DYZSiftController.h"
#import "DYZCustomModal.h"
#import "Masonry.h"
#import "DYZSiftLeftViewCell.h"
#import "DYZSiftRightViewCell.h"
#import "UIColor+Addition.h"



static NSString *leftCellID = @"leftCellID";
static NSString *rightCellID = @"rightCellID";
static NSString *normalCellID = @"normalCellID";
@interface DYZSiftController ()<UITableViewDelegate,UITableViewDataSource>


@property(nonatomic,weak)UITableView *leftView;
@property(nonatomic,weak)UITableView *rightView;

@end

@implementation DYZSiftController{
    DYZCustomModal *_cstModal;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.modalPresentationStyle = UIModalPresentationCustom;
        _cstModal = [[DYZCustomModal alloc]init];
        self.transitioningDelegate = _cstModal;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUI];
    //注册cell
//    [self.leftView registerClass:[DYZSiftLeftViewCell class] forCellReuseIdentifier:leftCellID];
//    [self.rightView registerClass:[DYZSiftRightViewCell class] forCellReuseIdentifier:rightCellID];
    //设置行高
//    self.leftView.rowHeight = 60;
//    self.rightView.rowHeight = 52;
    
    [self.leftView registerNib:[UINib nibWithNibName:@"DYZSiftLeftViewCell" bundle:nil] forCellReuseIdentifier:leftCellID];
    
    [self.rightView registerNib:[UINib nibWithNibName:@"DYZSiftRightViewCell" bundle:nil] forCellReuseIdentifier:rightCellID];
    [self.rightView registerClass:[UITableViewCell class] forCellReuseIdentifier:normalCellID];
    
    //默认选中某一行
    [self.leftView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
    [self tableView:self.leftView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
}


- (void)setUI
{
    //左侧的tableView
    UITableView *leftView = [[UITableView alloc]init];
    [self.view addSubview:leftView];
    leftView.backgroundColor = [UIColor colorWithHex:0xe8e8e8];
    
    leftView.dataSource = self;
    leftView.delegate = self;
    self.leftView = leftView;
    
    //让不显示数据的cell 不显示下划线
    leftView.tableFooterView = [[UIView alloc]init];
    //添加右侧的tableView
    UITableView *rightView = [[UITableView alloc]init];
    [self.view addSubview:rightView];
    
    rightView.dataSource = self;
    rightView.delegate = self;
    self.rightView = rightView;
    //下边的view
    UIView *bottomView = [[UIView alloc]init];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    
    //底部视图的两个按钮
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //设置左侧按钮
    
    //label 的富文本属性
//    NSString *str = @"确定(147条车源)";
//    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str];
//    
//    [attrStr addAttribute:NSFontAttributeName
//                    value:[UIFont systemFontOfSize:14.0f]
//                    range:NSMakeRange(0, 2)];
//    [attrStr addAttribute:NSFontAttributeName
//                    value:[UIFont systemFontOfSize:12.0f]
//                    range:NSMakeRange(2, 8)];
//
//    [attrStr addAttribute:NSForegroundColorAttributeName
//                    value:[UIColor colorWithWhite:0.973 alpha:1.000]
//                    range:NSMakeRange(0, 10)];
//    [leftButton.titleLabel setAttributedText:attrStr];
    //[leftButton.titleLabel sizeToFit];
    [leftButton setTitle:@"确定(147条车源)" forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    leftButton.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [leftButton setBackgroundColor:[UIColor colorWithHex:0x0073dd]];
    
    //设置右侧按钮
    [rightButton setTitle:@"X 清空筛选条件" forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor colorWithHex:0x0073dd] forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:14];

    
    [bottomView addSubview:leftButton];
    [bottomView addSubview:rightButton];
    
    
    //设置下边视图的布局
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(leftView.mas_bottom);
        make.bottom.left.right.equalTo(self.view);
        make.height.offset(50);
        
    }];
    
    //设置两个按钮的自动布局
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bottomView.mas_top);
        make.height.offset(50);
        make.width.mas_equalTo(self.view.bounds.size.width * 0.5);
    }];
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(0);
        make.height.mas_equalTo(50);
        make.width.equalTo(leftButton);
        make.top.offset(0);
    }];
    
    //设置按钮的监听事件
    [leftButton addTarget:self action:@selector(disappear) forControlEvents:UIControlEventTouchUpInside];
    [rightButton addTarget:self action:@selector(disappear) forControlEvents:UIControlEventTouchUpInside];
    
    //设置自动布局
    //左侧的tableView
    [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.view);
        make.width.offset(150);
        make.bottom.equalTo(bottomView.mas_top);
    }];
    //右侧的tableView
    [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftView.mas_right);
        make.right.top.offset(0);
        make.bottom.equalTo(bottomView.mas_top);
    }];

    
}

//按钮点击事件
- (void)disappear
{
    [self dismissViewControllerAnimated:YES completion:nil];
    if ([self.delegate respondsToSelector:@selector(dismissViewControllerAnimated:completion:)]) {
        [self.delegate disMissSiftController:self];
    }
}
#pragma mark - UITableViewDelegate 实现联动
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.rightView) {
        return;
    }else
    {
        [self.rightView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.row] animated:YES scrollPosition:UITableViewScrollPositionTop];
        
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if ((UITableView *)scrollView == self.leftView) {
        return;
    }else
    {
        [self.leftView selectRowAtIndexPath:[NSIndexPath indexPathForRow:self.rightView.indexPathsForVisibleRows.firstObject.section inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
    }

}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    [self scrollViewDidEndDecelerating:scrollView];
}

#pragma mark -UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.rightView) {
        return 4;
    }
    else
    {
        return 1;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.leftView) {
        return 4;
    }else
    {
        if (section == 0) {
            return 1;
        }else
        {
        return 10;
        }
    }
}

//cell的样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    if (tableView == self.leftView) {
     cell = [tableView dequeueReusableCellWithIdentifier:leftCellID forIndexPath:indexPath];
        cell.backgroundColor = [UIColor colorWithHex:0xe8e8e8];
        //设置cell 的选中颜色
        UIView *backgroundV = [[UIView alloc]initWithFrame:cell.bounds];
        backgroundV.backgroundColor = [UIColor whiteColor];
        cell.selectedBackgroundView = backgroundV;
        //cell.textLabel.text = [NSString stringWithFormat:@"%zd",indexPath.row];
        
    }else if(tableView == self.rightView && indexPath.section != 0)
    {
      cell = [tableView dequeueReusableCellWithIdentifier:rightCellID forIndexPath:indexPath];
        //cell.textLabel.text = [NSString stringWithFormat:@"%zd",indexPath.row];
        //设置cell 的选中颜色
        UIView *backgroundV = [[UIView alloc]initWithFrame:cell.bounds];
        backgroundV.backgroundColor = [UIColor colorWithWhite:0.937 alpha:1.000];
        cell.selectedBackgroundView = backgroundV;
       
    }else
    {
        cell = [tableView dequeueReusableCellWithIdentifier:normalCellID forIndexPath:indexPath];
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        cell.textLabel.textColor = [UIColor colorWithHex:0x0073dd];
        cell.textLabel.text = @"全部";
    }
    
    //DYZSiftLeftViewCell *cell = [tableView dequeueReusableCellWithIdentifier:leftCellID forIndexPath:indexPath];
   return cell;
}

//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.leftView) {
        return 60;
    }else
    {
        if (indexPath.section == 0) {
            return 36;
        }else
        {
            return 40;
        }
    }
}
//头部视图的样式
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *headerLabel = [[UILabel alloc]init];
    headerLabel.font = [UIFont systemFontOfSize:12];
    headerLabel.textColor = [UIColor colorWithHex:0x666666];
    headerLabel.backgroundColor = [UIColor colorWithHex:0xf2f2f2];
    if (tableView == self.rightView) {
        if (section == 1) {
            headerLabel.text = @"2015款";
        }else if (section == 2)
        {
             headerLabel.text = @"2013款";
        }
    }
    return headerLabel;
}
//头部视图的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (tableView == self.rightView && section != 0) {
        return 20;
    }
    return 0;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
